package com.android.djlmapp.adapter.order;

import android.content.Context;
import android.graphics.Color;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.model.order.OrderListInfo;

import org.refresh_view.pullrecyclerview.BaseRecyclerAdapter;
import org.refresh_view.pullrecyclerview.BaseViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @Author wangziheng
 * @ProjectName djlm
 * @ClassName OrderListAdapter
 * @package com.android.djlmapp.adapter
 * @data 2018/12/5 18:46
 * @Describe
 * @Change
 */
public class OrderListAdapter extends BaseRecyclerAdapter<OrderListInfo.DataBean> {

    public TextView orderStatusTv,mOrderNameTv;
    public OrderListAdapter(Context context){
        super(context,R.layout.order_list_item_layout,new ArrayList<>());
    }

    @Override
    protected void convert(BaseViewHolder holder, OrderListInfo.DataBean item) {
        orderStatusTv = holder.itemView.findViewById(R.id.order_list_orderStatus_tv);
        mOrderNameTv = holder.itemView.findViewById(R.id.order_list_orderName_tv);
        String[] orderStatusArray = getOrderStatusValue(item.getStatus(),item.getOrder_type());
        orderStatusTv.setTextColor(Color.parseColor(orderStatusArray[1]));
        orderStatusTv.setText(orderStatusArray[0]);
        holder.setText(R.id.order_list_orderSn_tv,"订单号:"+item.getOrder_sn());
        holder.setText(R.id.order_list_createOrderTime_tv,strToData(item.getAdd_time()));
    }

    public static final String COLOR_FF4081 = "#FF4081";
    public static final String COLOR_ff600a = "#ff600a";
    public static final String COLOR_00ba23 = "#00ba23";
    public static final String COLOR_b9b9b9 = "#b9b9b9";
    /**
     * 获取订单状态
     * 已提交   待付款    #FF4081  粉红色
     * 已完成  绿色   #00ba23
     * 已取消  已退款 #b9b9b9  灰色
     * 处理中（正处理、待处理）已发货  已付款  #ff600a	橙色
     * @param orderStatusCode  订单状态码
     * @param orderType  订单类型
     * @return
     * 0 订单状态值  1 订单状态颜色
     *
     * 默认是充值订单的  游戏充值 Q币充值
     */
    public String[] getOrderStatusValue(String orderStatusCode,String orderType){
        String[] orderValue = new String[2];
        switch (orderStatusCode){
            case "2":
            case "4":
            case "6":
            case "9":
                orderValue[0] = "处理中";
                orderValue[1] = COLOR_ff600a;
                break;
            case "5":
                orderValue[0] = "已完成";
                orderValue[1] = COLOR_00ba23;
                break;
            case "7":
                orderValue[0] = "已取消";
                orderValue[1] = COLOR_b9b9b9;
                break;
            default:
                orderValue[0] = "暂无状态";
                orderValue[1] = COLOR_FF4081;
                break;
        }
        return orderValue;
    }

    /**
     * 时间戳转换
     * @param strDate
     * @return
     */
    public String strToData(String strDate){
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(strDate);
        int i = Integer.parseInt(strDate);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    /**
     * 手机号截取
     * @param source
     * @param startIndex
     * @param step
     * @param separator
     * @return
     */
    public String separateString(String source, int startIndex, int step, char separator) {
        int times = 0;
        StringBuilder tmpBuilder = new StringBuilder(source);
        for (int i = 0; i < tmpBuilder.length(); i++) {
            if (i == startIndex + step * times + times) {
                if (separator != tmpBuilder.charAt(i)) {
                    tmpBuilder.insert(i, separator);
                }
                times++;
            } else {
                if (separator == tmpBuilder.charAt(i)) {
                    tmpBuilder.deleteCharAt(i);
                    i = -1;
                    times = 0;
                }
            }
        }
        return tmpBuilder.toString();
    }
}
